import logging

from flask import Blueprint, request

from app.Controllers.BaseController import BaseController
from app.Handler import MesManager
from app.Models.FloorVehicles import FloorVehicles
from app.Models.Model import FloorVehicle
from app.Vendor.Utils import Utils

floorVehicle = Blueprint('floorVehicleController', __name__)

log = logging.getLogger('error_msg')


@floorVehicle.route('/getVehicle', methods=['POST'])
def get_vehicle():
    vin = request.json.get('vin')
    vehicles = FloorVehicles()
    vehicle_info = vehicles.get_one_by_vin(vin)
    if not vehicle_info:
        vehicle_info = FloorVehicle()
    return Utils.format_date(vehicle_info.to_dict())


@floorVehicle.route('/getNextVehicle', methods=['POST'])
def get_next_vehicle():
    vin = request.json.get('vin')
    vehicles = FloorVehicles()
    vehicle_info = vehicles.get_one_by_vin(vin)
    if not vehicle_info:
        next_vehicle = FloorVehicle()
    else:
        next_vehicle = vehicles.get_next_vehicle(vehicle_info)
    return Utils.format_date(next_vehicle.to_dict())


@floorVehicle.route('/getNumber', methods=['POST'])
def get_number():
    start_vin = request.json.get('start_vin')
    end_vin = request.json.get('end_vin')

    vehicles = FloorVehicles()
    start_vehicle = vehicles.get_one_by_vin(start_vin)
    end_vehicle = vehicles.get_one_by_vin(end_vin)
    num = vehicles.get_number(start_vehicle, end_vehicle)

    return BaseController().success(msg='成功', data=num)


@floorVehicle.route('/getMesOrder', methods=['GET'])
def get_mes_order():
    MesManager.get_data()
    return BaseController().success(msg='成功')
